# 修复 Android SDK 版本兼容性问题
Write-Host "正在修复 Android SDK 版本兼容性问题..." -ForegroundColor Green

# 检查常见的 Android SDK 路径
$sdkPaths = @(
    "$env:USERPROFILE\AppData\Local\Android\Sdk",
    "C:\Android\Sdk", 
    "C:\Program Files\Android\Sdk",
    "C:\Android"
)

$androidSdkRoot = $null

foreach ($path in $sdkPaths) {
    if (Test-Path "$path\cmdline-tools\latest\bin\sdkmanager.bat") {
        Write-Host "找到 Android SDK: $path" -ForegroundColor Yellow
        $androidSdkRoot = $path
        break
    }
}

if (-not $androidSdkRoot) {
    Write-Host "未找到 Android SDK，请手动设置 ANDROID_SDK_ROOT 环境变量" -ForegroundColor Red
    Write-Host "常见的 SDK 路径:" -ForegroundColor Yellow
    Write-Host "- $env:USERPROFILE\AppData\Local\Android\Sdk" -ForegroundColor Cyan
    Write-Host "- C:\Android\Sdk" -ForegroundColor Cyan
    Write-Host "- C:\Program Files\Android\Sdk" -ForegroundColor Cyan
    Read-Host "按任意键退出"
    exit 1
}

Write-Host "设置 ANDROID_SDK_ROOT=$androidSdkRoot" -ForegroundColor Green

# 设置环境变量
$env:ANDROID_SDK_ROOT = $androidSdkRoot

# 更新 SDK 工具
Write-Host "正在更新 Android SDK 工具..." -ForegroundColor Yellow
& "$androidSdkRoot\cmdline-tools\latest\bin\sdkmanager.bat" --update

# 安装最新的 build-tools
Write-Host "正在安装最新的 build-tools..." -ForegroundColor Yellow
& "$androidSdkRoot\cmdline-tools\latest\bin\sdkmanager.bat" "build-tools;34.0.0"

# 安装最新的 platform-tools
Write-Host "正在安装最新的 platform-tools..." -ForegroundColor Yellow
& "$androidSdkRoot\cmdline-tools\latest\bin\sdkmanager.bat" "platform-tools"

# 安装 Android 34 平台
Write-Host "正在安装 Android 34 平台..." -ForegroundColor Yellow
& "$androidSdkRoot\cmdline-tools\latest\bin\sdkmanager.bat" "platforms;android-34"

Write-Host "SDK 更新完成！" -ForegroundColor Green
Write-Host "请重新启动 Android Studio 或命令行工具" -ForegroundColor Yellow
Read-Host "按任意键退出" 